package main

import (
	"crypto/sha256"
	"flag"
	"fmt"
	"go-sha256sum/util"
	"os"
)

func printSha256(path string) {
	strContent := util.ReadFileToStr(path)
	sum := sha256.Sum256([]byte(strContent))
	fmt.Printf("sha256: %x\n", sum)
}

var path = flag.String("path", "", "文件路径")

func main() {

	//验证操作文件路径是否有效
	osPath := os.Args
	realPath := ""
	if len(osPath) > 1 && len(osPath[1]) > 1 && osPath[1] != "-path" && osPath[1] != "--help" {
		realPath = osPath[1]
	} else {
		flag.Parse()
		flag, message := util.PathExists(*path)
		if !flag {
			fmt.Printf(message)
		} else {
			realPath = *path
		}
	}
	if realPath != "" {
		printSha256(realPath)
	}

}
